package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.util.Log;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Myapplication;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.CommonUtil;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.Constant;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.LogUtil;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.MapUtils;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.Trace;
import com.baidu.trace.api.entity.LocRequest;
import com.baidu.trace.api.track.HistoryTrackRequest;
import com.baidu.trace.api.track.HistoryTrackResponse;
import com.baidu.trace.api.track.OnTrackListener;
import com.baidu.trace.api.track.QueryCacheTrackResponse;
import com.baidu.trace.api.track.TrackPoint;
import com.baidu.trace.model.LocationMode;
import com.baidu.trace.model.OnCustomAttributeListener;
import com.baidu.trace.model.OnTraceListener;
import com.baidu.trace.model.Point;
import com.baidu.trace.model.ProtocolType;
import com.baidu.trace.model.PushMessage;
import com.baidu.trace.model.SortType;

import java.util.ArrayList;
import java.util.List;

public class TrailService extends Service {

    // 轨迹服务客户端
    public LBSTraceClient client;
    private Trace trace;
    private int serviceId = Constant.SERVICE_ID;
    private String entityName ;
    private OnTraceListener traceListener;
    private HistoryTrackRequest historyTrackRequest;
    private boolean isFirst=true;


    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String phone = null;
        if(intent !=null){
             phone = intent.getStringExtra("Phone");
        }
        if(!phone.isEmpty()){
            entityName=phone;
            initListener();
            init();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private void initListener() {

        traceListener = new OnTraceListener() {
            @Override
            public void onBindServiceCallback(int i, String s) {
                LogUtil.i("TRAIL","-onBindServiceCallback-------"+s);

            }

            @Override
            public void onStartTraceCallback(int i, String s) {
                LogUtil.i("TRAIL","-onStartTraceCallback-------"+s);
            }

            @Override
            public void onStopTraceCallback(int i, String s) {
                LogUtil.i("TRAIL","--------"+s);

            }

            @Override
            public void onStartGatherCallback(int i, String s) {
                LogUtil.i("TRAIL","--onStartGatherCallback------"+s);

            }

            @Override
            public void onStopGatherCallback(int i, String s) {
                LogUtil.i("TRAIL","-onStopGatherCallback-------"+s);

            }

            @Override
            public void onPushCallback(byte b, PushMessage pushMessage) {
                String message = pushMessage.getMessage();
                LogUtil.i("TRAIL","-onPushCallback-------"+message);

            }
        };

        historyTrackRequest = new HistoryTrackRequest();
        // 开始时间(单位：秒)
        long startTime = System.currentTimeMillis() / 1000 - 12 * 60 * 60;;
        // 结束时间(单位：秒)
        long endTime = System.currentTimeMillis() / 1000;
        // 设置开始时间
        historyTrackRequest.setStartTime(startTime);
        // 设置结束时间
        historyTrackRequest.setEndTime(endTime);
        historyTrackRequest.setEntityName(entityName);
        historyTrackRequest.setPageIndex(1);
        historyTrackRequest.setPageSize(5000);
        historyTrackRequest.setServiceId(serviceId);

    }

    private void init() {
        getApplication();
        Myapplication appInstance = (Myapplication)getApplication();;
        client= appInstance.client;
        if(client == null){
            return;
        }

        //client=new LBSTraceClient(getApplicationContext());
        trace=new Trace(serviceId,entityName,false);
        client.setInterval(5,10);// 定位周期，打包回传周期
        client.setLocationMode(LocationMode.High_Accuracy);//定位模式
        // client.setProtocolType(ProtocolType.HTTPS);
        client.setOnTraceListener(traceListener);
        //开启服务
        client.startTrace(trace,traceListener);
        //开启采集
        client.startGather( traceListener);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        client.stopTrace(trace,traceListener);
        client.stopGather(traceListener);
    }
}
